package com.nova.scenic.projectlibs.util.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nova.scenic.projectlibs.R;
import com.nova.scenic.projectlibs.util.adapter.SimpleRecyclerAdapter.SimpleViewHold;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by scenic on 16/6/7.
 */
public class SimpleRecyclerAdapter<T,ViewHold extends SimpleViewHold> extends
        RecyclerView.Adapter<ViewHold> {

    private Context mContext;

    private List<T> mData;

    private AdapterView.OnItemClickListener listener;

    public SimpleRecyclerAdapter(Context context) {
        mContext = context;
    }

    public Context getContext() {
        return mContext;
    }

    public void updateAll(List<T> d) {
        if (mData == null) {
            mData = d;
        } else {
            mData.clear();
            mData.addAll(d);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if(mData != null){
            mData.clear();
        }
    }


    public void addFirst(T t){
        if(mData == null){
            mData = new ArrayList<>();
        }
        mData.add(0,t);
        notifyItemChanged(0);
    }
    public void addLast(T t){
        if(mData == null){
            mData = new ArrayList<>();
        }

        mData.add(t);
        notifyItemChanged(mData.size() -1);
    }

    private SparseArray<DisplayUIEntity> typeSparseArray;

    public void setTypeSparseArray(SparseArray<DisplayUIEntity> array){
        this.typeSparseArray = array;
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        if(typeSparseArray != null){
            DisplayUIEntity messageDisplayEntity = typeSparseArray.get(viewType);
            return (ViewHold) messageDisplayEntity.onCreateViewHolder(parent, viewType);
        }else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.simple_list_item_two_text, null);
            ViewHold vh = (ViewHold) new SimpleViewHold(view,viewType);
            return vh;
        }

    }


    @Override
    public void onBindViewHolder(ViewHold holder, int position) {
        if(typeSparseArray != null){
            DisplayUIEntity messageDisplayEntity = typeSparseArray.get(getItemViewType(position));
            messageDisplayEntity.onBindViewHolder(holder, position);
        }else {
            if (holder instanceof SimpleViewHold) {
                initView(holder.mImageView,
                        holder.mText1,
                        holder.mText2,
                        mData.get(position)
                );
            }
        }

    }

    protected void initView(ImageView iv, TextView tx1, TextView tx2, T t) {
        tx1.setText(t.toString());
    }

    @Override
    public int getItemCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }


    public T getItem(int position) {
        if (mData == null) {
            return null;
        }
        return mData.get(position);
    }


    @Override
    public int getItemViewType(int position) {

//        String messageClassType = getItem(position).getMessageBody().getClassType();
//        return classTypeToInt(messageClassType);
//        setTypeSparseArray(Integer.parseInt(StringUtil.hashCode(T.class.getName())),new DisplayUIEntity());



        return super.getItemViewType(position);
    }

    public static class SimpleViewHold extends RecyclerView.ViewHolder {
        public TextView mText1 = null;
        public TextView mText2 = null;
        public ImageView mImageView = null;
        public int viewType;
        public SimpleViewHold(View itemView,int viewType) {
            super(itemView);
            this.viewType = viewType;
            mText1 = (TextView) itemView.findViewById(R.id.id_txview_scenic_util_1);
            mText2 = (TextView) itemView.findViewById(R.id.id_txview_scenic_util_2);
            mImageView = (ImageView) itemView.findViewById(R.id.id_imgview_scenic_util_1);
        }
    }

    public interface DisplayUIEntity<ViewHold extends SimpleViewHold> {
        ViewHold onCreateViewHolder(ViewGroup parent, int viewType);
        void onBindViewHolder(ViewHold holder, int position);
    }
}
